﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using CNotebook;

namespace NotebookS2
{
    public partial class CRYSTAL_SO_LOG : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public CRYSTAL_SO_LOG()
        {
            InitializeComponent();
        }

        ReportDocument rptDoc = new ReportDocument();
        SqlConnection oConnection = new SqlConnection();
        SqlCommand oCommand = new SqlCommand();
        SqlDataAdapter oAdapter = new SqlDataAdapter();
        DataSet ds = new DataSet();

        public string ConnectionString { get; set; }
        public DateTime FromDate { get; set; }
        public DateTime ToDate { get; set; }

        public bool WithDraft { get; set; }
        public bool WithInProcess { get; set; }
        public bool WithPosted { get; set; }
        public bool WithOpen { get; set; }
        public bool WithClosed { get; set; }

        public string GetLayout()
        {
            ReportConfigManager oManager = new ReportConfigManager();
            const int _DOCUMENT_TYPE = 40;
            oManager.ConnectionString = this.ConnectionString;

            oManager.Open();

            string _FILE_NAME = oManager.GetReportFileName(_DOCUMENT_TYPE);

            oManager.Close();

            return _FILE_NAME;
        }

        public void ViewSalesOrderLogs()
        {
            rptDoc = new ReportDocument();
            oConnection = new SqlConnection();
            oCommand = new SqlCommand();
            oAdapter = new SqlDataAdapter();
            ds = new DataSet();

            string sReportFile = "";

            sReportFile = GetLayout();

            rptDoc.Load(sReportFile);

            oConnection.ConnectionString = this.ConnectionString;
            oConnection.Open();

            oCommand.Connection = oConnection;
            oCommand.CommandType = CommandType.StoredProcedure;
            oCommand.CommandText = "RPT_SO_LOG";

            oCommand.Parameters.Add(new SqlParameter("@FROM", this.FromDate));
            oCommand.Parameters.Add(new SqlParameter("@TO", this.ToDate));

            oCommand.Parameters.Add(new SqlParameter("@WITHDRAFT", Convert.ToInt16(this.WithDraft)));
            oCommand.Parameters.Add(new SqlParameter("@INPROCESS", Convert.ToInt16(this.WithInProcess)));
            oCommand.Parameters.Add(new SqlParameter("@WITHPOSTED", Convert.ToInt16(this.WithPosted)));
            oCommand.Parameters.Add(new SqlParameter("@WITHOPEN", Convert.ToInt16(this.WithOpen)));
            oCommand.Parameters.Add(new SqlParameter("@WITHCLOSED", Convert.ToInt16(this.WithClosed)));

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(ds);

            oConnection.Close();

            rptDoc.SetDataSource(ds.Tables[0]);


            CRViewer.ReportSource = rptDoc;
            
            CRViewer.Refresh();
        }

        private void CRYSTAL_SALESORDER_Load(object sender, EventArgs e)
        {
            ViewSalesOrderLogs();
        }

        private void ribbonControl1_Click(object sender, EventArgs e)
        {

        }

        private void CRYSTAL_SO_LOG_FormClosing(object sender, FormClosingEventArgs e)
        {
            rptDoc.Dispose();
            oConnection.Dispose();
            oCommand.Dispose();
            oAdapter.Dispose();
            ds.Dispose();
        }
    }
}
